
package com.wyzz.hr.bo;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.dao.IDepartmentDAO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;


public class OrgDeptMergeHelper extends OrgDeptOperateHelper {

	public OrgDeptMergeHelper(Department fromDept, Department toDept) {
		dao = (IDepartmentDAO) SpringBeanFactory.getBean("departmentDAO");
		deptBo = (IDepartmentBO) SpringBeanFactory.getBean("departmentBO");
		posBo = (IPositionBo) SpringBeanFactory.getBean("positionBo");
		pbBo = (IPositionBaseBo) SpringBeanFactory.getBean("positionBaseBo");
		logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		setFromDept(fromDept);
		setToDept(toDept);
	}

	public String cancelResp(String respPosId) {
		String info = "SUCC";
		Position fromRespPos = posBo.getPosById(respPosId, new String[0]);
		Position toRespPos = posBo.getRespPosOfDept(toDept.getId());
		List fromSubPosList = posBo.getDirectSubPos(fromRespPos.getId());
		for (int i = 0; fromSubPosList != null && i < fromSubPosList.size(); i++) {
			Position pos = (Position) fromSubPosList.get(i);
			pos.setPositionParentId(toRespPos);
		}

		dao.saveOrupdate(fromSubPosList);
		return info;
	}

	public String respPBCopy(String pbId) {
		Position fromRespPos = posBo.getRespPosOfDept(fromDept.getId());
		Position toRespPos = posBo.getRespPosOfDept(toDept.getId());
		PositionBase newPb = pbBo.copyPbToAnotherDept(pbId, toDept.getId(),
				getCurrentEmp().getId());
		fromRespPos.setPositionParentId(toRespPos);
		fromRespPos.setPositionPbId(newPb);
		dao.saveOrupdate(fromRespPos);
		if (fromRespPos.getPositionEmpNo() != null
				&& !StringUtils.isEmpty(fromRespPos.getPositionEmpNo().getId()))
			deptBo.saveEmpTransferData(fromRespPos.getPositionEmpNo(), toDept,
					newPb);
		return "SUCC";
	}

	public String respPBMoveTo(String pbId, String toPBId) {
		PositionBase toPB = pbBo.getPBById(toPBId);
		Position fromRespPos = posBo.getRespPosOfDept(fromDept.getId());
		Position toRespPos = posBo.getRespPosOfDept(toDept.getId());
		if (fromRespPos.getPositionEmpNo() != null
				&& !StringUtils.isEmpty(fromRespPos.getPositionEmpNo().getId())) {
			Employee emp = fromRespPos.getPositionEmpNo();
			String info = posBo.batchSetEmpPos(toRespPos.getId(), toPBId,
					new String[] { fromRespPos.getPositionEmpNo().getId() });
			if (!"SUCC".equals(info))
				return info;
			deptBo.saveEmpTransferData(emp, toDept, toPB);
		}
		PositionBase fromRespPB = fromRespPos.getPositionPbId();
		fromRespPB.setPbStatus(new Integer(0));
		operateDisablePB(fromRespPB);
		return "SUCC";
	}

	public String pbCopy(String pbId) {
		PositionBase pb = pbBo.getPBById(pbId);
		PositionBase newPb = pbBo.copyPbToAnotherDept(pbId, toDept.getId(),
				getCurrentEmp().getId());
		List posList = posBo.getPosListByPB(pb);
		for (int i = 0; posList != null && i < posList.size(); i++) {
			Position tempPos = (Position) posList.get(i);
			tempPos.setPositionPbId(newPb);
			dao.saveOrupdate(tempPos);
			if (tempPos.getPositionEmpNo() != null
					&& !StringUtils.isEmpty(tempPos.getPositionEmpNo().getId()))
				deptBo.saveEmpTransferData(tempPos.getPositionEmpNo(), toDept,
						newPb);
		}

		return "SUCC";
	}

	public String pbMoveTo(String pbId, String toPBId) {
		PositionBase pb = pbBo.getPBById(pbId);
		PositionBase toPB = pbBo.getPBById(toPBId);
		List posList = posBo.getPosListByPB(pb);
		List empIdList = new ArrayList();
		for (int i = 0; posList != null && i < posList.size(); i++) {
			Position tempPos = (Position) posList.get(i);
			if (tempPos.getPositionEmpNo() != null
					&& !StringUtils.isEmpty(tempPos.getPositionEmpNo().getId()))
				empIdList.add(tempPos.getPositionEmpNo().getId());
		}

		String hql = (new StringBuilder()).append(
				"delete from Position where positionPbId.id='").append(toPBId)
				.append("' and positionEmpNo.id is null").toString();
		dao.exeHql(hql);
		for (int i = 0; posList != null && i < posList.size(); i++) {
			Position tempPos = (Position) posList.get(i);
			tempPos.setPositionPbId(toPB);
			if (tempPos.getPositionEmpNo() != null
					&& !StringUtils.isEmpty(tempPos.getPositionEmpNo().getId()))
				deptBo.saveEmpTransferData(tempPos.getPositionEmpNo(), toDept,
						toPB);
		}

		dao.saveOrupdate(posList);
		pb.setPbStatus(new Integer(0));
		String tempInfo = operateDisablePB(pb);
		if (!"SUCC".equals(tempInfo))
			return tempInfo;
		else
			return "SUCC";
	}

	public String deptOperate() {
		List fromSubDeptList = deptBo.getSubDeptsByParentId(fromDept.getId());
		for (int i = 0; fromSubDeptList != null && i < fromSubDeptList.size(); i++) {
			Department tempDept = (Department) fromSubDeptList.get(i);
			deptBo.saveDeptMove(tempDept.getId(), toDept.getId());
		}

		fromDept.setDepartmentStatus(new Integer(0));
		String tempInfo = operateDisableCurrentDept(fromDept);
		if (!"SUCC".equals(tempInfo))
			return tempInfo;
		else
			return "SUCC";
	}

	public Department getToDept() {
		return toDept;
	}

	public void setToDept(Department toDept) {
		this.toDept = toDept;
	}

	public Department getFromDept() {
		return fromDept;
	}

	public void setFromDept(Department fromDept) {
		this.fromDept = fromDept;
	}

	private Department fromDept;
	private Department toDept;
	private IDepartmentDAO dao;
	private IDepartmentBO deptBo;
	private IPositionBo posBo;
	private IPositionBaseBo pbBo;
	private ISysLogBO logBO;
}
